package com.fcgzs.springboot.job;

import com.fcgzs.springboot.redisconstant.RedisConstant;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Set;

//清理图片(套餐信息管理)的定时任务
@Component
public class CleanPicJob {
    //获取redis操作工具
    @Resource
    private RedisTemplate<String,String> redisTemplate;
    //cron表达式：CronTrigger触发器
    @Scheduled(cron = "0/5 * * * * ?")
    public void cleanTast(){
        //获取本地图片的集合key，获取数据库的图片集合key
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        Set<String> difference = setOperations.difference(RedisConstant.SETMEAL_PIC_UPLOAD, RedisConstant.SETMEAL_PIC_DB);
        if(difference.size() > 0){
            for(String picName: difference){
                //删除本地图片
                File file = new File("E:\\缓存区\\上传路径\\upload\\" + picName);
                file.delete();
                //删除集合中的图片信息
                setOperations.remove(RedisConstant.SETMEAL_PIC_UPLOAD,picName);
            }
        }
    }
}
